﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TekaMUD.Engine.Blocks;
using TekaMUD.Engine.IO;

namespace TekaMUD.Engine.Commands
{
	public class MovementCommands
	{
		public static void Go_North(Command c)
		{
			GenericMovement(c, EnumDirectionType.North);
		}

		public static void Go_South(Command c)
		{
			GenericMovement(c, EnumDirectionType.South);
		}

		public static void Go_East(Command c)
		{
			GenericMovement(c, EnumDirectionType.East);
		}

		public static void Go_West(Command c)
		{
			GenericMovement(c, EnumDirectionType.West);
		}

		public static void Go_Up(Command c)
		{
			GenericMovement(c, EnumDirectionType.Up);
		}

		public static void Go_Down(Command c)
		{
			GenericMovement(c, EnumDirectionType.Down);
		}

		public static void GenericMovement(Command c, EnumDirectionType direction)
		{
			Room startRoom = c.Player.CurrentRoom;
			Room endRoom = null;
			RoomConnection usedConnection = null;

			usedConnection = startRoom.GetConnection(direction);

			if(usedConnection == null)
			{
				
				Messenger.SendAgent(c.Player, "You can't go that way.\r\n");
				return;
			}

			endRoom = usedConnection.End;

			// process doors.
			if (usedConnection != null && usedConnection.Door != null && !usedConnection.Door.Opened)
			{
				Messenger.SendAgent(c.Player, "There is a closed door to the " + direction.ToString() + "\r\n");
				return;
			}

			using (new ChannelOutputHolder(c.Channel))
			{
				if (startRoom.ExitMessages != null)
					Messenger.SendAgent(c.Player, startRoom.ExitMessages);

				World.Current.AssignPlayerToRoom(endRoom, c.Player);
				Messenger.SendAgent(c.Player, "You move " + direction.ToString() + ".\r\n");
				Messenger.SendAgent(c.Player, endRoom.EnterMessage);

				if (c.Player.AutoLook)
					ExplorationActions.AutoLook(c);

				if (c.Player.Invisible)
					Messenger.SendRoom(endRoom, "You feel a presence nearby...");
				else
					Messenger.SendRoom(endRoom, string.Format("{0} enters the room.", c.Player.Name), c.Player);
			}
		}
	}
}
